/*--------------------------------------------------------------------------------
	DESCRIPTION: Cleaning IVS

--------------------------------------------------------------------------------*/

clear
use "$data_dir/raw/Integrated_values_surveys_1981-2022.dta"

clonevar iso2 = S009 
replace S009 = "GB" if S009=="GB-NIR" | S009=="GB-GBN" | S009=="NIR" 
replace S009 = "XK" if S009=="RS-KM"

merge m:1 S009 using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop if _merge==1 //drops Northern Cyprus
drop _merge

replace iso2 = S009

gen survey = "EVS" if S001==1
replace survey = "WVS" if S001==2

gen wave = S002 if survey=="WVS"
replace wave = S002EVS if survey=="EVS"

gen weight = S017

**********************************
*************TRUST VALUES*********
**********************************

gen trust_govt = 1 if E069_11==1|E069_11==2
replace trust_govt = 0 if E069_11==3|E069_11==4

gen trust_govt_dk_ref = 1 if E069_11==.a|E069_11==.b //"don't know" or "refused" option
replace trust_govt_dk_ref = 0 if trust_govt_dk_ref==. & E069_11!=. 

gen trust_justice = 1 if E069_17==1|E069_17==2
replace trust_justice = 0 if E069_17==3|E069_17==4

gen trust_police = 1 if E069_06==1|E069_06==2
replace trust_police = 0 if E069_06==3|E069_06==4

gen trust_civil_service = 1 if E069_08==1|E069_08==2
replace trust_civil_service = 0 if E069_08==3|E069_08==4

gen trust_local_govt = .

gen trust_parties = 1 if E069_12==1|E069_12==2
replace trust_parties = 0 if E069_12==3|E069_12==4

gen trust_parliament = 1 if E069_07==1|E069_07==2
replace trust_parliament = 0 if E069_07==3|E069_07==4

gen trust_others = 1 if A165==1
replace trust_others = 0 if A165==2 

gen trust_military = 1 if E069_02==1|E069_02==2
replace trust_military = 0 if E069_02==3|E069_02==4

gen trust_religious_organizations = 1 if E069_01==1|E069_01==2
replace trust_religious_organizations = 0 if E069_01==3|E069_01==4

pca trust_govt trust_justice trust_police trust_parties trust_parliament, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

gen survey_year = S020

clonevar yob = X002
replace yob = -99 if yob==.

gen age = survey_year-yob if yob!=-99

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			1   Male               
*			2   Female                                
*		  -99   Missing          

clonevar sex = X001
replace sex = -99 if sex==.a | sex==.b | sex==.c | sex==.d | sex==.e
lab def sex 1 "Male" 2 "Female" -99 "Missing"
lab val sex sex

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if F025==0
replace religion = 2 if F025==7
replace religion = 3 if F025==4
replace religion = 4 if F025==1|F025==2|F025==3|F025==8
replace religion = 5 if F025==5
replace religion = -99 if religion==.a | religion==.b | religion==.c | religion==.d | religion==.e
replace religion = 6 if religion==. & F025!=.
replace religion = 7 if F025==6

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = . 
replace edu = 1 if X025==2
replace edu = 2 if X025==3|X025==4|X025==5|X025==6
replace edu = 3 if X025==7|X025==8
replace edu = 4 if X025==1
replace edu = -99 if edu==.

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

cap drop isco088 isco88 job
tostring X035_2, gen(isco088)
tostring X035_2_01, gen(isco88)
tostring X036, gen(job)
replace isco088 = "" if isco088==".a" | isco088==".b" | isco088==".c" | isco088==".d" | isco088==".e"
replace isco88 = "" if isco88==".a" | isco88==".b" | isco88==".c" | isco88==".d" | isco88==".e"
replace job = "" if job==".a" | job==".b" | job==".c" | job==".d" | job==".e"
replace job = "" if isco088!="" | isco88!=""
replace job = substr(job,1,1) if job!=""

cap drop occupation
gen occupation = ""
replace occupation = substr(isco088,1,1) if occupation==""
replace occupation = substr(isco88,1,1) if occupation==""

destring occupation, replace
replace occupation = 1 if job=="1"
replace occupation = 2 if job=="2"
replace occupation = 8 if job=="3"
replace occupation = 6 if job=="4"
replace occupation = 0 if job=="5"
replace occupation = . if job=="6" | job=="8"
replace occupation = -99 if occupation==.

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = 1 if inlist(X028, 1, 2, 3)
replace employment_status = 2 if inlist(X028, 7)
replace employment_status = 3 if inlist(X028, 4, 5, 6, 8)
replace employment_status = -99 if employment_status==.

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		   11   Band 11
*		  -99   Missing

gen income = .
replace income = X047_EVS
replace income = 10 if X047_EVS==11
replace income = -99 if income==.a | income==.b | income==.c | income==.d | income==.e
replace income = X047_WVS if income==-99
replace income = -99 if income==.a | income==.b | income==.c | income==.d | income==.e

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

clonevar ideology = E181C
replace ideology = -99 if ideology==. | ideology==44

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = .
replace marital = 1 if X007==1|X007==2
replace marital = 2 if X007==3|X007==4|X007==5|X007==6
replace marital = -99 if marital==.

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = 1 if X050C==1
replace urban = 2 if X050C==2|X050C==3
replace urban = -99 if urban==.

*born_country

gen born_country = 1 if G017==1|X002_02==1
replace born_country = 0 if (G017>1 & G017<=8) | X002_02==0

keep S009 ccode country numcode country_short cow wave trust_state trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military trust_religious_organizations survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban survey born_country weight 

sort ccode wave survey_year

save "$data_dir/clean/ivs_clean.dta", replace
